﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MapLib
{
    class generator
    {
    }

    class common
    {
        static int[,] map;

        public static void setMap(int[,] Map)
        {
            map = Map;
        }

        /// <summary>
        /// Count all the closed cells around the specified cell and return that number
        /// </summary>
        /// <param name="xVal">cell X value</param>
        /// <param name="yVal">cell Y value</param>
        /// <returns>Number of surrounding cells</returns>
        public static int examineNeighbours(int xVal, int yVal)
        {
            int count = 0;

            for (int x = -1; x < 2; x++)
            {
                for (int y = -1; y < 2; y++)
                {
                    if (checkCell(xVal + x, yVal + y) == true)
                        count += 1;
                }
            }

            return count;
        }

        /// <summary>
        /// Check the examined cell is legal and closed
        /// </summary>
        /// <param name="x">cell X value</param>
        /// <param name="y">cell Y value</param>
        /// <returns>Cell state - true if closed, false if open or illegal</returns>
        public static Boolean checkCell(int x, int y)
        {
            if (x >= 0 & x < map.GetLength(0) &
                y >= 0 & y < map.GetLength(1))
            {
                if (map[x, y] > 0)
                    return true;
                else
                    return false;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// Check the examined cell is legal and open
        /// </summary>
        /// <param name="x">cell X value</param>
        /// <param name="y">cell Y value</param>
        /// <returns>Cell state - true if closed, false if open or illegal</returns>
        public static Boolean checkCellOpen(int x, int y)
        {
            if (x >= 0 & x < map.GetLength(0) &
                y >= 0 & y < map.GetLength(1))
            {
                if (map[x, y] == 0)
                    return true;
                else
                    return false;
            }
            else
            {
                return false;
            }
        }
    }

    class method1
    {
        private Random r = new Random(System.DateTime.Now.Millisecond);

        private int neighbours = 4;
        private int iterations = 50000;
        bool probExceeded = true;
        private int mapX = 50;
        private int mapY = 50;
        private int closeCellProb = 60;
        private int[,] map;

        public int Neighbours
        {
            get { return neighbours; }
            set { neighbours = value; }
        }

        public int Iterations
        {
            get { return iterations; }
            set { iterations = value; }
        }

        public int MapX
        {
            get { return mapX; }
            set { mapX = value; }
        }

        public int MapY
        {
            get { return mapY; }
            set { mapY = value; }
        }

        public int CloseCellProb
        {
            get { return closeCellProb; }
            set { closeCellProb = value; }
        }

        public bool ProbExceeded
        {
            get { return probExceeded; }
            set { probExceeded = value; }
        }




        /// <summary>
        /// Build a map
        /// </summary>
        /// <param name="closeCellProb">Probability of closing a cell</param>
        /// <param name="neighbours">The number of cells required to trigger</param>
        /// <param name="iterations">Number of iterations</param>
        /// <param name="Map">map array to opearate on</param>
        /// <param name="reset">Clear the map before operation</param>
        /// <param name="probExceeded">probability exceeded</param>
        /// <param name="invert"></param>
        /// <returns></returns>
        public int[,] go()
        {

            map = new int[mapX, MapY];


            //go through each cell and use the specified probability to determine if it's open
            for (int x = 0; x < map.GetLength(0); x++)
            {
                for (int y = 0; y < map.GetLength(1); y++)
                {
                    if (r.Next(0, 100) < closeCellProb)
                    {
                        map[x, y] = 1;
                    }
                }
            }

            //common.setMap(map);

            //pick some cells at random
            for (int x = 0; x <= iterations; x++)
            {
                int rX = r.Next(0, map.GetLength(0));
                int rY = r.Next(0, map.GetLength(1));

                if (probExceeded == true)
                {
                    if (examineNeighbours(rX, rY) > neighbours)
                    {
                        map[rX, rY] = 1;
                    }
                    else
                    {
                        map[rX, rY] = 0;
                    }
                }
                else
                {
                    if (examineNeighbours(rX, rY) > neighbours)
                    {
                        map[rX, rY] = 0;
                    }
                    else
                    {
                        map[rX, rY] = 1;
                    }
                }


            }

            return map;

        }

        /// <summary>
        /// Count all the closed cells around the specified cell and return that number
        /// </summary>
        /// <param name="xVal">cell X value</param>
        /// <param name="yVal">cell Y value</param>
        /// <returns>Number of surrounding cells</returns>
        private int examineNeighbours(int xVal, int yVal)
        {
            int count = 0;

            for (int x = -1; x < 2; x++)
            {
                for (int y = -1; y < 2; y++)
                {
                    if (checkCell(xVal + x, yVal + y) == true)
                        count += 1;
                }
            }

            return count;
        }

        /// <summary>
        /// Check the examined cell is legal and closed
        /// </summary>
        /// <param name="x">cell X value</param>
        /// <param name="y">cell Y value</param>
        /// <returns>Cell state - true if closed, false if open or illegal</returns>
        private Boolean checkCell(int x, int y)
        {
            if (x >= 0 & x < map.GetLength(0) &
                y >= 0 & y < map.GetLength(1))
            {
                if (map[x, y] > 0)
                    return true;
                else
                    return false;
            }
            else
            {
                return false;
            }
        }

    }
}
